﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Exercitation.Model;
using Yipusoft.Exercitation.BLL;

public partial class Combat_Receive : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }

    private void Bind()
    {
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        if (user == null)
            return;
        bool bl = false;
       chkDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList("DepartmentID IN( Select DepartmentID From FireBase..Soldiers Where Left(DepartmentID,4)='" + user.DepartmentID.Substring(0, 4) + "' And IsDelete='0') And DepartmentID Not IN(Select DepartmentID From TaskSquadron Where TaskID='" + Request["TaskID"] + "' And TaskBrigadeID ='" + Request["TaskBrigadeID"] + "') ");
       chkDepartment.DataTextField = "FullName";
       chkDepartment.DataValueField = "DepartmentID";
       chkDepartment.DataBind();
       foreach (ListItem item in chkDepartment.Items)
       {
           item.Selected = true;
           bl = true;
       }
       btnOk.Visible = bl;

       chkReceive.DataSource = Yipusoft.Account.BLL.Department.GetList("DepartmentID IN (Select DepartmentID From TaskSquadron Where Left(DepartmentID,4)='" + user.DepartmentID.Substring(0, 4) + "' And TaskID='" + Request["TaskID"] + "' And TaskBrigadeID ='" + Request["TaskBrigadeID"] + "' )");
       chkReceive.DataTextField = "FullName";
       chkReceive.DataValueField = "DepartmentID";
       chkReceive.DataBind();
       foreach (ListItem item in chkReceive.Items)
       {
           item.Selected = true;
       }
       chkReceive.Enabled = false;

    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in chkDepartment.Items)
        {
            if (item.Selected)
            {
                TaskSquadronInfo info = new TaskSquadronInfo();
                info.AcceptDate = DateTime.MaxValue;
                info.ID = Guid.NewGuid();
                info.IsAccept = false;
                info.TaskID = new Guid(Request["TaskID"]);
                info.DepartmentID = item.Value;
                info.AcceptUser = "";
                info.TaskBrigadeID = new Guid(Request["TaskBrigadeID"]);
                TaskSquadron.Insert(info);

            }
        }

        TaskBrigadeInfo brigade= TaskBrigade.GetTaskBrigade(new Guid(Request["TaskBrigadeID"]));
        if (brigade != null)
        {
            brigade.IsOn = true;
            brigade.SetOnDate = DateTime.Now;
            brigade.SetOnUser = User.Identity.Name;
            TaskBrigade.Update(brigade);
        }
        Bind();

        Page.RegisterStartupScript("close", "<script language=\"javascript\" type=\"text/javascript\">  if (confirm('任务下达成功,是否关闭当前窗口?')) close();</script>");
        
    }
}
